<?php
/**
 * 员工产量工资的审核页面，相当于财务过账功能
 */ 
FLEA::loadClass('TMIS_Controller');
class Controller_CaiWu_Confirm extends Tmis_Controller {
	var $_modelExample;
	var $funcId = 148;
	function Controller_CaiWu_Confirm() {
		/*if(!$this->authCheck()) die("禁止访问!");*/
		
		$this->leftCaption = '员工产量审核';		
		$this->_modelExample = & FLEA::getSingleton('Model_CaiWu_Wages');

        $this->_modelRanseChanliang = & FLEA::getSingleton('Model_Chejian_RanseChanliang');
        $this->_modelGang = & FLEA::getSingleton('Model_Plan_Dye_Gang');
        $this->_modelOrder = & FLEA::getSingleton('Model_Trade_Dye_Order');
	}	
	
	function actionListGuozhang(){
		$this->authCheck($this->funcId);
		 FLEA::loadClass("TMIS_Pager");
        $arr = &TMIS_Pager::getParamArray(array(
            "dateFrom" => date('Y-m-01'),
            "dateTo" => date('Y-m-d'),
            'orderCode'=>'',
            'danhao'=>'',
            'proCode'=>'',
            'clientId'=>'',
        ));
        $sql="select * from (
               	 select x.id,x.dateInput,x.gangId,x.cntK,x.banci,x.workerCode,x.danjia,(x.cntK*x.danjia) as money,'st' as kind,y.vatNum,
               	 		z.orderCode,z.clientId,
						w.wareName,w.guige,w.id as productId,
						v.color,v.colorNum,v.orderId,v.wareId,u.compName
                        from dye_st_chanliang x
                        left join plan_dye_gang y on x.gangId=y.id
                        left join trade_dye_order2ware v on y.order2wareId=v.id
						left join trade_dye_order z on v.orderId = z.id
						left join jichu_ware w on v.wareId=w.id
						left join jichu_client u on u.id=z.clientId
                        left join caiwu_wages_guozhang c on c.chanliangId=x.id
                        where 1  and c.id is null
                        group by x.id
                union

                select x.id,x.dateInput,x.gangId,x.cntK,x.banci,x.workerCode,x.danjia,(x.cntK*x.danjia)as money,'zcl' as kind,y.vatNum,
                		z.orderCode,z.clientId,
						w.wareName,w.guige,w.id as productId,
						v.color,v.colorNum,v.orderId,v.wareId,u.compName
                        from dye_zcl_chanliang x
                        left join plan_dye_gang y on x.gangId=y.id
                        left join trade_dye_order2ware v on y.order2wareId=v.id
						left join trade_dye_order z on v.orderId = z.id
						left join jichu_ware w on v.wareId=w.id
						left join jichu_client u on u.id=z.clientId
                        left join caiwu_wages_guozhang c on c.chanliangId=x.id
                        where 1 and c.id is null
                        group by x.id
                union

                select x.id,x.dateInput,x.gangId,x.cntK,x.banci,x.workerCode,x.danjia,(x.cntK*x.danjia)as money,'hs' as kind,y.vatNum,
                		z.orderCode,z.clientId,
						w.wareName,w.guige,w.id as productId,
						v.color,v.colorNum,v.orderId,v.wareId,u.compName
                        from dye_hs_chanliang x
                        left join plan_dye_gang y on x.gangId=y.id
                        left join trade_dye_order2ware v on y.order2wareId=v.id
						left join trade_dye_order z on v.orderId = z.id
						left join jichu_ware w on v.wareId=w.id
						left join jichu_client u on u.id=z.clientId
                        left join caiwu_wages_guozhang c on c.chanliangId=x.id
                        where 1 and c.id is null
                        group by x.id
                union

                select x.id,x.dateInput,x.gangId,x.cntK,x.banci,x.workerCode,x.danjia,(x.cntK*x.danjia)as money,'hd' as kind,y.vatNum,
                		z.orderCode,z.clientId,
						w.wareName,w.guige,w.id as productId,
						v.color,v.colorNum,v.orderId,v.wareId,u.compName
                        from dye_hd_chanliang x
                        left join plan_dye_gang y on x.gangId=y.id
                        left join trade_dye_order2ware v on y.order2wareId=v.id
						left join trade_dye_order z on v.orderId = z.id
						left join jichu_ware w on v.wareId=w.id
						left join jichu_client u on u.id=z.clientId
                        left join caiwu_wages_guozhang c on c.chanliangId=x.id
                        where 1 and c.id is null
                        group by x.id
                union

                select x.id,x.dateInput,x.gangId,x.cntK,x.banci,x.workerCode,x.danjia,(x.cntK*x.danjia)as money,'db' as kind,y.vatNum,
                		z.orderCode,z.clientId,
						w.wareName,w.guige,w.id as productId,
						v.color,v.colorNum,v.orderId,v.wareId,u.compName
                        from dye_db_chanliang x
                        left join plan_dye_gang y on x.gangId=y.id
                        left join trade_dye_order2ware v on y.order2wareId=v.id
						left join trade_dye_order z on v.orderId = z.id
						left join jichu_ware w on v.wareId=w.id
						left join jichu_client u on u.id=z.clientId
                        left join caiwu_wages_guozhang c on c.chanliangId=x.id
                        where 1 and c.id is null
                        group by x.id
            ) as a where 1 and danjia>0";

        if($arr['dateFrom'] !=''){
            $sql.=" and dateInput>='{$arr['dateFrom']}' and dateInput<='{$arr['dateTo']}'";
        }

        if($arr['orderCode']!='') $sql .=" and orderCode like '%{$arr['orderCode']}%' ";
        if($arr['clientId']!='') $sql .=" and clientId='{$arr['clientId']}' ";
        // $sql.=" order by chukuDate desc,orderCode desc";
        $pager = &new TMIS_Pager($sql);
        $rowset = $pager->findAll();
        // dump($rowset);//exit;

        foreach ($rowset as & $v) {
            $hj['_edit']='合计';
            $v['danjia']=round($v['danjia'],3);
            $v['money']=round($v['money'],2);
            $v['cntK']=round($v['cntK'],2);
            $hj['cntK'] += $v['cntK'];
            $hj['cntK'] = round($hj['cntK']);
            $v['_edit']="<input type='checkbox' id='chk[]' name='chk[]' value='chanliangId:{$v['id']},cntK:{$v['cntK']},orderId:{$v['orderId']},clientId:{$v['clientId']},productId:{$v['productId']},chanliangDate:{$v['dateInput']},vatNum:{$v['vatNum']},gangId:{$v['gangId']},kind:{$v['kind']},danjia:{$v['danjia']},money:{$v['money']},banci:{$v['banci']},workerCode:{$v['workerCode']}'/>";
           
            $heji += $v['money'];
            $hj['money']="<div id='heji' name='heji'>$heji</div>";
           
            $kindArr = array(
                'st'=>'松筒',
                'zcl'=>'装出笼',
                'hs'=>'烘纱',
                'hd'=>'回倒',
                'db'=>'打包',
            );
            $v['kind'] = isset($kindArr[$v['kind']])?$kindArr[$v['kind']]:$v['kind'];
        }
        $rowset[] = $hj;
        $arrFieldInfo = array(
            "_edit"     => array('text'=>"<input type='checkbox' id='checkedAll' title='全选/反选'/>",'width'=>40),
            "compName"  => '客户',
            "orderCode" => '订单号',
            "vatNum"    => '缸号',
            "wareName"  => '纱织规格',
            "kind"      => ' 类型',
            "color"     => '颜色',
            "cntK"      => '数量',
            "danjia"    => '单价',
            "money"     => '发生金额',
        );

        $smarty = &$this->_getView();
        $smarty->assign('title', '收发存报表');
        // $smarty->assign('pk', $this->_modelDefault->primaryKey);
        $smarty->assign('arr_field_info', $arrFieldInfo);
        $smarty->assign('add_display', 'none');
        $smarty->assign('arr_condition', $arr);
        $smarty->assign('arr_field_value', $rowset);
        $other_url="<button type='button' class='btn btn-info btn-sm' id='save2' name='save2'>保存</button>";
        $smarty->assign('other_url', $other_url);
        $smarty->assign('sonTpl', 'Caiwu/Confirm.tpl');
        $smarty->assign("page_info", $pager->getNavBar($this->_url($_GET['action'], $arr)));
        $smarty->display('TableList.tpl');
	}
	

	function actionSave() {
        // dump($_GET);die;
        $ids=json_decode($_GET['ids']);

        foreach ($ids as $key=>&$v){
            $arr=array();
            $temp=explode(',',$v);
            foreach ($temp as &$t){
                $tempt=explode(':',$t);
                $arr[$tempt[0]]=$tempt[1].'';
            }
            $arr['guozhangDate']=date('Y-m-d');
            $arr['creater']=$_SESSION['REALNAME'];
            // dump($arr);exit;
            $id=$this->_modelExample->save($arr);
        }
        echo json_encode('true');exit;
	}

	function actionRemove() {
		$pk=$this->_modelExample->primaryKey;
		$this->_modelExample->removeByPkv($_GET[id]);		
		redirect($this->_url("Right"));
	}

    function actionRight(){
        $this->authCheck('149');
        FLEA::loadClass('TMIS_Pager');
        $arr = TMIS_Pager::getParamArray(array(
            'dateFrom' =>date("Y-m-d",mktime(0,0,0,date("m"),1,date("Y"))),
            'dateTo' => date("Y-m-d"),
            'clientId'=>'',
            'vatNum'=>'',
            'orderCode'=>'',
            'workerCode'=>''
        ));
    
        // if ($arrGet[accountItemId]>0) $condition[] = array('accountItemId', $arrGet[accountItemId]);

        $model = FLEA::getSingleton('Model_CaiWu_Wages');

        $sql = "SELECT x.*,y.vatNum,
                z.orderCode,z.clientId,
                w.wareName,w.guige,w.id as productId,
                v.color,v.colorNum,v.orderId,v.wareId,u.compName
                from caiwu_wages_guozhang x
                left join plan_dye_gang y on x.gangId=y.id
                left join trade_dye_order2ware v on y.order2wareId=v.id
                left join trade_dye_order z on v.orderId = z.id
                left join jichu_ware w on v.wareId=w.id
                left join jichu_client u on u.id=z.clientId
                where 1
        ";
         if($arr['dateFrom'] !=''){
            $sql.=" and x.guozhangDate>='{$arr['dateFrom']}' and x.guozhangDate<='{$arr['dateTo']}'";
        }

        if($arr['orderCode']!='') $sql .=" and z.orderCode like '%{$arr['orderCode']}%' ";
        if($arr['workerCode']!='') $sql .=" and x.workerCode = '{$arr['workerCode']}' ";
        if($arr['vatNum']!='') $sql .=" and y.vatNum like '%{$arr['vatNum']}%' ";
        if($arr['clientId']!='') $sql .=" and z.clientId='{$arr['clientId']}' ";

        // dump($sql);die;
        $pager =& new TMIS_Pager($sql);
        $rowset =$pager->findAll();     
        // dump($rowset);die;


        if (count($rowset)>0) foreach($rowset as & $v) {
            $kindArr = array(
                'st'=>'松筒',
                'zcl'=>'装出笼',
                'hs'=>'烘纱',
                'hd'=>'回倒',
                'db'=>'打包',
            );
            $v['kind'] = isset($kindArr[$v['kind']])?$kindArr[$v['kind']]:$v['kind'];

            $v['_edit'] = $this->getRemoveHtml($v['id']);
        }
        $heji = $this->getHeji($rowset,array('cntK','money'),'_edit');
        $rowset[] = $heji;

        $smarty = & $this->_getView();
        $smarty->assign('title', $this->title);
        $arr_field_info = array(
            "_edit"         =>  "操作",
            "guozhangDate"  =>  "过账日期",
            "chanliangDate" =>  "产量日期",
            "orderCode"     =>  "订单",
            "wareName"      =>  "纱织规格",
            "kind"          =>  "类型",
            "banci"         =>  "班次",
            "workerCode"    =>  "员工编号",
            "cntK"          =>  "数量",
            "danjia"        =>  "单价",
            "money"         =>  "金额",
            // "memo"          =>  "备注"
        );
        $smarty->assign('title',$this->title);
        $smarty->assign('arr_field_info',$arr_field_info);
        $smarty->assign('arr_field_value',$rowset);
        $smarty->assign('add_display', 'none');
        $smarty->assign('arr_condition', $arr);
        $smarty->display('TableList.tpl');
    }

    function actionListRsPrice(){
        $this->authCheck('150');
        //列出已登记的产量。
        FLEA::loadClass('TMIS_Pager');
        
        $this->_modelRanseChanliang->enableLink('VatView');
        $arr = TMIS_Pager::getParamArray(array(
            'dateFrom'=>date('Y-m-d'),
            'dateTo'=>date('Y-m-d'),
            'workerCode'=>'',
            'vatNum'=>'',
            'banci'=>'',
        ));
        $condition=array();
        if($arr['dateFrom']!='')$condition[]=array('dateInput',$arr['dateFrom'],'>=');
        if($arr['dateTo']!='')$condition[]=array('dateInput',$arr['dateTo'],'<=');
        if($arr['workerCode']!='') $condition[] = array('workerCode',"%{$arr[workerCode]}%",'like');
        if($arr['vatNum']!='') $condition[] = array('Vat.vatNum',"%$arr[vatNum]%",'like');
        if($arr['banci']!='') $condition[] = array('banci',$arr['banci'],'=');

        $pager = new TMIS_Pager($this->_modelRanseChanliang,$condition);
        $rowset = $pager->findAll();

        foreach($rowset as &$v) {
            //dump($v);exit;
            $v[kindName] = $this->_modelRanseChanliang->getKindName($v[chanliangKind]);
            if ($v[chanliangKind]>0) {
                $v[cntTongzi] = "<font color='red'>$v[cntTongzi]</font>";
                $v[kindName] = "<font color='red'>$v[kindName]</font>";
            }
            $con = array("gangId='$v[gangId]'");
            $gang = $this->_modelGang->findAllGang1($con);
            if(count($gang)>0) $v = array_merge($v,$gang[0]);
            if($v['type']) $v[vatNum] = $v[Vat][vatNum]."(".$v['type'].")";
        }
        //dump($rowset);
        foreach($rowset as &$v){
            $v['guige']=$v['wareName'].' '.$v['guige'];
            $v['vatNum']=$this->_modelGang->setVatNum($v['VatView']['gangId'],$v['VatView']['order2wareId']);
            $v['orderCode']=$this->_modelOrder->getOrderTrack($v[orderId],$v['orderCode']);
            //得到客户单号
            $clientCode=$this->_modelOrder->find(array('id'=>$v['orderId']));
            if($clientCode['orderCode2']!='') $v['compName']=$v['compName'].'('.$clientCode['orderCode2'].')';
            $arrGang=$this->_modelGang->find(array('id'=>$v['gangId']));
            $str="select sum(cntK) as cntK from $this->tn where gangId='{$v['gangId']}'";
            $re=mysql_fetch_assoc(mysql_query($str));
            if($arrGang['rsOver']==1&&$arrGang['cntPlanTouliao']>$re['cntK'])$v['_edit']="<a href='".$this->_url('CancelOver',array('id'=>$v['gangId']))."'>取消完成</a>  ";

            $v['_edit'] = "<a href='".$this->_url('Supplement',array('id'=>$v['id']))."'>登记工序产量</a>";
        }
        $heji=$this->getHeji($rowset,array('cntK'),'dateInput');
        $rowset[]=$heji;
        $smarty = $this->_getView();
        //dump($rowset[0]);exit;
        $smarty->assign('arr_field_info',array(
            'dateInput'=>'日期',
            //dt=>'录入时间',
            'compName'=>"客户(客户单号)",
            'orderCode'=>"订单号",
            'vatNum'=>"缸号",
            'guige'=>"纱支",
            'color'=>"颜色",
            'banci'=>"班次",
            'workerCode'=>'工号',
            'Vat.cntPlanTouliao' =>'投料数',
            'cntTongzi'=>'筒子数',
            'cntK'=>'公斤数',
            '_edit'=>'操作'
        ));

        /*$smarty->assign('arr_edit_info',array(
            "edit" =>"修改",
            "remove" =>"删除"
        ));*/
        $smarty->assign('title','染色产量查询');
        $smarty->assign('arr_field_value',$rowset);
        $smarty->assign('add_display','none');
        $smarty->assign('arr_condition',$arr);
        $smarty->assign('page_info',$pager->getNavBar($this->_url($_GET['action'])));
        $smarty->display('TableList.tpl');
    }

    //修改产量
    function actionSupplement() {
        $arr=$this->_modelRanseChanliang->find($_GET[id]);
        $arr[Gang] = $this->_modelGang->find($arr[gangId]);
        $arr[Client] = $this->_modelGang->getClient($arr[gangId]);
        $arr[Ware] = $this->_modelGang->getWare($arr[gangId]);

        $rsGxCount = count($arr['rsGxCl']);
        for ($i=0; $i < 5-$rsGxCount; $i++) { 
            $arr['rsGxCl'][] = array();
        }
        $smarty = $this->_getView();
        $smarty->assign('aRow',$arr);
        $smarty->assign('showRsGx',true);
        $smarty->display('Chejian/RsChanliangEdit.tpl');
    }


    //保存产量
    function actionSaveChanliang() {
        // dump($_POST);exit;
        $arr = $_POST;
        $arr['dateInput'] = date("Y-m-d");
        //处理双染情况
        $gang = $this->_modelGang->find(array('id'=>$arr['gangId']));

        //整合该缸的染色工序单价
        $this->_modelVat = &FLEA::getSingleton('Model_JiChu_Vat');
        $vatDetail = $this->_modelVat->find(array('id'=>$gang['Vat']['id']));
        if(count($vatDetail['RsgxPrice']>0)){
            $RsgxPrice = array();
            foreach ($vatDetail['RsgxPrice'] as $k => &$v) {
                $RsgxPrice[$v['gxId']]['price'] = $v['price'];
            }
        }

        //dump($gang);exit;
        if ($gang['markTwice']==1){
            if($gang['fensanOver']==0) {//如果分散没有完成,表示产量是第一道工序分散产量
                //判断是否双染分配在同一班做掉
                if($gang['dateAssign']==$gang['dateAssign1'] && $gang['ranseBanci']==$gang['ranseBanci1']) {
                    $sql = "update plan_dye_gang set fensanOver=2 where id='{$arr['gangId']}'";
                    $arr['type']='分套同班';
                } else {
                    $sql = "update plan_dye_gang set fensanOver=1 where id='{$arr['gangId']}'";
                    $arr['type']='分散';
                }
            } else {
                $sql = "update plan_dye_gang set fensanOver=2 where id='{$arr['gangId']}'";
                $arr['type']='套棉';
            }
            //echo $sql;exit;
            mysql_query($sql) or die(mysql_error());
        }
        $temp = array();
        foreach ($_POST['gxId'] as $key => &$v) {
            if(!$v['gxId']) continue;
            $temp[$key]['id'] = $_POST['rs2gxId'][$key];
            $temp[$key]['gxId'] = $_POST['gxId'][$key];
            $temp[$key]['cntK'] = $_POST['cnt'][$key]+0;
            if($RsgxPrice[$_POST['gxId'][$key]]){
                $temp[$key]['danjia'] = $RsgxPrice[$_POST['gxId'][$key]]['price'];
                $temp[$key]['money'] = $temp[$key]['danjia'];
            }else{
                $temp[$key]['danjia'] = $_POST['danjia'][$key];
                $temp[$key]['money'] = $_POST['money'][$key]>0?$_POST['money'][$key]:$_POST['danjia'][$key];
            }
        }
        unset($arr['gxId']);
        unset($arr['cnt']);
        unset($arr['rs2gxId']);
        $arr['rsGxCl'] = $temp;
        // dump($arr);exit;
        $this->_modelRanseChanliang->save($arr);
    

        if($arr['biaoji']){           /*$re['cntK']>=$arrGang['cntPlanTouliao']||*/
            $kk=array(
                'id'=>$arr['gangId'],
                'rsOver'=>1
            );  
        }else{
            $kk=array(
                'id'=>$arr['gangId'],
                'rsOver'=>0
            );  
        }
        $this->_modelGang->save($kk);
        js_alert('保存成功!','',$this->_url('ListRsPrice'));
    }


    function actionListRsReport(){
        $this->authCheck('151');
        FLEA::loadClass('TMIS_Pager');
        $arrGet = TMIS_Pager::getParamArray(array(
            dateFrom =>date("Y-m-d",mktime(0,0,0,date("m"),1,date("Y"))),
            dateTo => date("Y-m-d"),
            'workerCode'=>'',
            //vatNum=>'',
            //orderCode=>''
        ));
        $sql = "SELECT x.*,
                y.workerCode,y.type,y.banci,
                z.gxName
                from dye_rs_chanliang2gxdetail x
                left join dye_rs_chanliang y on x.rsClId=y.id
                left join jichu_rsprice z on z.id=x.gxId
                where 1";
        if($arrGet['workerCode']!='') $sql.=" and y.workerCode like '%{$arrGet['workerCode']}%'";
        $sql .=" group by y.workerCode,x.gxId,y.banci";
        $clArr=$this->_modelRanseChanliang->findBysql($sql);
        // dump($clArr);die;

        if (count($clArr)>0) foreach($clArr as & $v) {
       
        }


      

        //设置模板
        $smarty = & $this->_getView();
        $smarty->assign('title', $this->title);
        $arr_field_info = array(
            "workerCode" =>"工号",
            "banci" =>"班次",
            "gxName" => "工序名称",
            // "cntK"   => "总产量",
            "money" => "金额",
        
        );
        $smarty->assign('title',$this->title);
        $smarty->assign('compCode',$arr[compCode]);
        $smarty->assign('arr_field_info',$arr_field_info);
        $smarty->assign('arr_field_value',$clArr);
        $smarty->assign('add_display', 'none');
        $smarty->assign('arr_condition', $arrGet);
        $smarty->display('TableList.tpl');
    }

}
?>